package com.changpay.zywy.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.changpay.zywy.R;
import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;

/**
 * Created by AndroidIntexh1 on 2018/12/26.
 */

public class SelectCountTypeDialog extends BaseCustomDialog {

    public OnDialogClickListener dialogListener;
    private LinearLayout ll_one,ll_two,ll_three;
    private TextView tv_one,tv_two,tv_three;
    private ImageView iv_one,iv_two,iv_three;
    private View vClose;
    private static final String KEY_TYPE= "KEY_TYPE";
    private TextView tvTitle;

    /**
     * 自定义builder来增加一些参数，记得要继承自BaseEasyDialog.Builder
     */
    public static class Builder extends BaseEasyDialog.Builder<SelectCountTypeDialog.Builder> {
        private Bundle bundle = new Bundle();
        private SelectCountTypeDialog.OnDialogClickListener listener;

        public Builder(@NonNull Context context) {
            super(context);
        }



        public SelectCountTypeDialog.Builder setOnDialogClickListener(SelectCountTypeDialog.OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        @NonNull
        @Override
        protected SelectCountTypeDialog createDialog() {
            SelectCountTypeDialog dialog = new SelectCountTypeDialog();
            dialog.setArguments(bundle);
            dialog.dialogListener = listener;
            return dialog;
        }

        public SelectCountTypeDialog.Builder setType(int type) {
            bundle.putInt(KEY_TYPE, type);
            return this;
        }


    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_select_count_type;
    }

    @Override
    protected void bindViews(View view) {
        ll_one = findView(R.id.ll_count_one);
        ll_two = findView(R.id.ll_count_two);
        ll_three = findView(R.id.ll_count_three);

        iv_one = findView(R.id.iv_type_one);
        iv_two = findView(R.id.iv_type_two);
        iv_three = findView(R.id.iv_type_three);

        tv_one = findView(R.id.tv_type_one);
        tv_two = findView(R.id.tv_type_two);
        tv_three = findView(R.id.tv_type_three);

        vClose = findView(R.id.v_close);
        tvTitle = findView(R.id.tv_count_title);




    }

    private void setListener() {
        vClose.setOnClickListener(v -> dismiss());
    }

    private void setData() {
        assert getArguments() != null;
        switch (getArguments().getInt(KEY_TYPE,0))
        {
            case 0:
                tvTitle.setText("总量图");
                tv_one.setText("交易量");
                tv_two.setText("激活量");
                tv_three.setText("收益量");
                iv_one.setBackgroundResource(R.mipmap.icon_count_one);
                iv_two.setBackgroundResource(R.mipmap.icon_count_two);
                iv_three.setBackgroundResource(R.mipmap.icon_count_three);
                ll_one.setOnClickListener(v -> {
                    dialogListener.onClick(v,0);
                    dismiss();
                });
                ll_two.setOnClickListener(v -> {
                    dialogListener.onClick(v,1);
                    dismiss();
                });
                ll_three.setOnClickListener(v -> {
                    dialogListener.onClick(v,2);
                    dismiss();
                });
                break;
            case 1:
                tvTitle.setText("走势图");
                tv_one.setText("交易量");
                tv_two.setText("激活量");
                tv_three.setText("收益量");
                iv_one.setBackgroundResource(R.mipmap.icon_count_four);
                iv_two.setBackgroundResource(R.mipmap.icon_count_five);
                iv_three.setBackgroundResource(R.mipmap.icon_count_six);

                ll_one.setOnClickListener(v -> {
                    dialogListener.onClick(v,3);
                    dismiss();
                });
                ll_two.setOnClickListener(v -> {
                    dialogListener.onClick(v,4);
                    dismiss();
                });
                ll_three.setOnClickListener(v -> {
                    dialogListener.onClick(v,5);
                    dismiss();
                });
                break;
            case 2:
                tvTitle.setText("激活率");
                tv_one.setText("总激活率");
                tv_two.setText("月激活率");
                iv_one.setBackgroundResource(R.mipmap.icon_count_seven);
                iv_two.setBackgroundResource(R.mipmap.icon_count_eight);
                ll_three.setVisibility(View.GONE);
                ll_one.setOnClickListener(v -> {
                    dialogListener.onClick(v,6);
                    dismiss();
                });
                ll_two.setOnClickListener(v -> {
                    dialogListener.onClick(v,7);
                    dismiss();
                });

                break;
            case 3:
                tvTitle.setText("月交易量");
                tv_one.setText("月交易量");
                tv_two.setText("商户交易量");
                tv_three.setText("渠道交易量");
                iv_one.setBackgroundResource(R.mipmap.icon_count_nine);
                iv_two.setBackgroundResource(R.mipmap.icon_count_ten);
                iv_three.setBackgroundResource(R.mipmap.icon_count_eleven);

                ll_one.setOnClickListener(v -> {
                    dialogListener.onClick(v,8);
                    dismiss();
                });
                ll_two.setOnClickListener(v -> {
                    dialogListener.onClick(v,9);
                    dismiss();
                });
                ll_three.setOnClickListener(v -> {
                    dialogListener.onClick(v,10);
                    dismiss();
                });
                break;
            case 4:
                tvTitle.setText("新增统计");
                tv_one.setText("新增商户");
                tv_two.setText("新增渠道");
                iv_one.setBackgroundResource(R.mipmap.icon_count_twelve);
                iv_two.setBackgroundResource(R.mipmap.icon_count_thirteen);
                ll_three.setVisibility(View.GONE);
                ll_one.setOnClickListener(v -> {
                    dialogListener.onClick(v,11);
                    dismiss();
                });
                ll_two.setOnClickListener(v -> {
                    dialogListener.onClick(v,12);
                    dismiss();
                });

                break;
        }
    }
    @Override
    protected void setViews() {
        setData();
        setListener();
        setBackground();
        setLayout();

    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();

        // 强制宽高
        int padding = getResources().getDimensionPixelOffset(R.dimen.dialog_count_padding);
        lp.width = getScreenWidth(getActivity()) - (padding * 2);
//        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);

        lp.gravity = Gravity.CENTER; // 设置展示的位置
//        lp.y = getResources().getDimensionPixelOffset(R.dimen.join_shop_dialog_margin);
        window.setAttributes(lp);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, int type);
    }


}
